package musicstore.web.controller;

import java.awt.print.Book;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import musicstore.biz.AlbumBiz;
import musicstore.biz.GenreBiz;
import musicstore.entity.Album;

@Controller
public class BuylistController {
	@Autowired
	private AlbumBiz albumbiz;
	@Autowired
	private GenreBiz genrebiz;

	@RequestMapping("/order-list")
	public String getlist(HttpSession session, Album album, Model model,Integer count) {
		count = count==null?0:count;
		
		System.out.println(album.getTitle());
		List<Album> albumlist = (List<Album>) session.getAttribute("albumlist");
		if (albumlist == null) {
			albumlist = new ArrayList<Album>();
			count =1;
		}else{
			count=count+1;
		}
		albumlist.add(album);
		// 放到购物车
		
		System.out.println(count);
		
		// 把购物车放到 Session
		session.setAttribute("albumlist", albumlist);
		model.addAttribute("count",count);
		model.addAttribute("categories", genrebiz.getAll());
		return "order-list";
	}
	@RequestMapping("/delete")
	public String delete(HttpSession session, Integer id){
		id = id==null?0:id;
		System.out.println(id);
		List<Album> albumlist = (List<Album>) session.getAttribute("albumlist");
		if(id>0){
			albumlist.remove(id);
		}
		
		return "order-list";
	}

}
